import { Node, Vec3 } from 'cc';
import EventExt from 'db://cocos-ext/base/EventExt';
import { UtilCocos } from 'db://cocos-ext/base/UtilCocos';
import { players, rooms } from 'yycore';

import YYExt from '../../yyExt/YYExt';
import { SLOT_BET_VALUE_CHANGE, SLOT_SPIN_START } from './SlotEventType';

/** 一些押注UI的公用数据 */
export default class SlotBetModule {

    // 单例
    private static _ins: SlotBetModule = null;
    private constructor() { }
    static getIns(): SlotBetModule {
        if (!SlotBetModule._ins)
            SlotBetModule._ins = new SlotBetModule();
        return SlotBetModule._ins;
    }



    /** 获取当前服务器指定的kv值 */
    private getCurRoomKV(key: string, def?: string): string {
        const info = rooms.clientSite.getCurrentRoomServerInfo();
        if (!info) return "";
        return info.getKeyString(key, def);
    }
    /** 解析服务器约定的数组字符串 */
    private parseNumAry(data: string): number[] {
        if (!data || data.trim().length == 0)
            return [];
        let ary: number[] = [];
        data = data.trim();
        const strList: string[] = data.split(',');
        strList.forEach(str => {
            // 防止首尾异常分隔符
            if (str != undefined && str != null) {
                const n = parseInt(str);
                if (n != undefined)
                    ary.push(n);
            }
        });
        return ary;
    }

    /** 房间配置 -- ShowRS_LockChips -- 押注档位的解锁等级列表 */
    static get lockChips(): number[] {
        if (!this.getIns()._lockChips) {
            const str = this.getIns().getCurRoomKV("ShowRS_LockChips");
            this.getIns()._lockChips = this.getIns().parseNumAry(str);
        }
        return this.getIns()._lockChips;
    }
    /** 获取 lockChips 指定下标的值 */
    static getLockChips(idx: number): number {
        const len = this.lockChips.length;
        if (len == 0)
            return 0;
        if (idx < 0)
            return this.lockChips[0];
        else if (idx >= len)
            return this.lockChips[len - 1]
        else
            return this.lockChips[idx];
    }
    private _lockChips: number[] = null;


    /** 房间配置 -- ShowRS_Chips -- 押注档位列表 */
    static get betChips(): number[] {
        if (!this.getIns()._betChips) {
            const str = this.getIns().getCurRoomKV("ShowRS_Chips");
            this.getIns()._betChips = this.getIns().parseNumAry(str);
        }
        return this.getIns()._betChips;
    }
    /** 获取 betChips 指定下标的值 */
    static getBetChips(idx: number): number {
        const len = this.betChips.length;
        if (len == 0)
            return 0;
        if (idx < 0)
            return this.betChips[0];
        else if (idx >= len)
            return this.betChips[len - 1]
        else
            return this.betChips[idx];
    }
    private _betChips: number[] = null;

    /** 最大已解锁的押注档位 */
    static get maxUnlockBetLv(): number {
        let lv = 0;
        const curLv = players.myself.getExpLevel().level;
        const betLen = this.betChips.length;
        for (let i = betLen - 1; i >= 0; i--) {
            if (curLv >= this.getLockChips(i)) {
                lv = i;
                break;
            }
        }
        return lv;
    }
    /** 最大可用的押注档位，解锁+钱足够 */
    static get maxVaildBetLv(): number {
        let lv = 0;
        const money = YYExt.getMoney();
        const curLv = players.myself.getExpLevel().level;
        const betLen = this.betChips.length;
        for (let i = betLen - 1; i >= 0; i--) {
            if (curLv >= this.getLockChips(i) && money >= this.betChips[i]) {
                lv = i;
                break;
            }
        }
        return lv;
    }

    /** 当前UI下注档位 */
    static get curUiBetLv(): number {
        if (this.getIns()._curUiBetLv == undefined) {
            // 初始化; 设计要求：查询当前最大可用押注额
            this.getIns()._curUiBetLv = this.maxVaildBetLv;
            EventExt.emit(SLOT_BET_VALUE_CHANGE);
        }
        return this.getIns()._curUiBetLv;
    }
    static set curUiBetLv(i: number) {
        this.getIns()._curUiBetLv = i;
        EventExt.emit(SLOT_BET_VALUE_CHANGE);
    }
    private _curUiBetLv: number;


    /** 当前UI押注额 */
    static get betValue(): number {
        return this.getBetChips(this.curUiBetLv);
    }


    /** 自动下注回合数 */
    static get autoBetCnt(): number {
        return this.getIns()._autoBetCnt;
    }
    static set autoBetCnt(v: number) {
        this.getIns()._autoBetCnt = v;
    }
    /** 是否还有自动押注次数 */
    static get hasAutoBet(): boolean {
        return this.getIns()._autoBetCnt > -1;
    }
    private _autoBetCnt: number = -1;



    /**
     * 开启 slot转盘 操作；
     * 内置钱判定+弹窗提示；
     * 发送事件 SLOT_SPIN_START
     * @returns 
     */
    static spinStart() {
        if (this.betValue <= 0)
            return;
        if (!YYExt.checkMoney(this.betValue)) {
            // TODO showalm
            return;
        }
        EventExt.emit(SLOT_SPIN_START, this.betValue);
    }


    /** 获取输赢文本世界坐标 */
    static get winLoseWPos(): Vec3 {
        return UtilCocos.getNodeWPos(this.getIns()._winLoseNode);
    }
    /** 更新输赢文本节点--动态获取坐标用 */
    static set winLoseNode(n: Node) {
        this.getIns()._winLoseNode = n;
    }
    private _winLoseNode: Node = null;

}